#!/bin/bash

DIR="$( cd "$( dirname "$0" )" && pwd )"

source $DIR/env.sh
source $DIR/deployment/$EXPLOCATION/$EXPTYPE/experiment-variables.sh

#THREADS="250 500 750"

export RUNEXP_EXEC="run-experiments.sh"

RUN_MAIN=1

RUN_NODES=1

#DBG_ARGS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"
DBG_ARGS=''

JVM_ARGS="$DBG_ARGS -Dcamid.ssh.user=$USER -Dcamid.ssh.key=$TARGET_LOCATION/$SSH_KEY_NAME -Dcamid.main.node=$MAINSERVER -Dcamid.work.nodes=$COLON_SEP_NODES -Dcamid.port=15001 -Dcamid.scripts.startnode=$TARGET_LOCATION/start-node.sh -Dcamid.scripts.stopnode=$TARGET_LOCATION/stop-node.sh"

SSH_OPTS='-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'

echo "nodes: $NODES"

if [ $RUN_MAIN -ne 0 ] ; then
	echo "Starting java processes from $MAINSERVER"
	ssh $SSH_OPTS -i $SSH_KEY $USER@$MAINSERVER "nohup java $DBG_ARGS -jar $TARGET_LOCATION/$JAR -port $DEFAULT_PORT -ns $MGMT_OPTS 2> $TARGET_LOCATION/out.log > $TARGET_LOCATION/out.log < /dev/null &"
	#ssh -i $SSH_KEY $USER@$MAINSERVER "java -jar $TARGET_LOCATION/$JAR -port $DEFAULT_PORT -ns -cm "
fi

if [ $RUN_NODES -ne 0 ] ; then 
	for node in $NODES; do
		echo "Starting java processes from $node"
		ssh $SSH_OPTS -i $SSH_KEY $USER@$node "nohup java $DBG_ARGS -jar $TARGET_LOCATION/$JAR -port $DEFAULT_PORT -nsport $DEFAULT_PORT -nshost $MAINSERVER 2> $TARGET_LOCATION/out.log > $TARGET_LOCATION/out.log < /dev/null &"
	done
fi

for client in $JMETERCLIENT; do
	echo "Starting java processes from $client"
   #echo "cd $TARGET_LOCATION/$JMETER_DIR ; rm -v *.log ; export JVM_ARGS=\"$JVM_ARGS\" ; export TEST_OBJECT=\"$TEST_OBJECT\" ; export TESTCASE=\"$TESTCASE\" ; chmod +x $TARGET_LOCATION/$JMETER_DIR/$RUNEXP_EXEC ; nohup $TARGET_LOCATION/$JMETER_DIR/$RUNEXP_EXEC > $TARGET_LOCATION/run-experiments.out.log 2> $TARGET_LOCATION/run-experiments.err.log < /dev/null &"
	ssh $SSH_OPTS -i $SSH_KEY $USER@$client "cd $TARGET_LOCATION/$JMETER_DIR ; rm -v *.log ; export JVM_ARGS=\"$JVM_ARGS\" ; export TEST_OBJECT=\"$TEST_OBJECT\" ; export TESTCASE=\"$TESTCASE\" ; chmod +x $TARGET_LOCATION/$JMETER_DIR/$RUNEXP_EXEC ; nohup $TARGET_LOCATION/$JMETER_DIR/$RUNEXP_EXEC > $TARGET_LOCATION/run-experiments.out.log 2> $TARGET_LOCATION/run-experiments.err.log < /dev/null & "
done


